package com.dkd.testRank.service.impl;

import com.dkd.testRank.domain.TestInformation;
import com.dkd.testRank.domain.TestRank;
import com.dkd.testRank.mapper.TestRankMapper;
import com.dkd.testRank.service.ITestRankService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * rank排行 服务实现类
 * </p>
 *
 * @author zwy
 * @since 2025-05-19
 */
@Service
public class TestRankServiceImpl implements ITestRankService {
    @Autowired
    private TestRankMapper testRankMapper;

    @Override
    public List<TestRank> selectList(TestRank testRank) {
        return testRankMapper.selectList(testRank);
    }

    @Override
    public TestRank selectById(Integer id) {
        return testRankMapper.selectById(id);
    }

    @Override
    @Transactional
    public int insertTestRank(TestRank testRank) {
        testRankMapper.insertTestRank(testRank);
        return insertTestInformation(testRank);
    }

    @Override
    @Transactional
    public int deleteTestRanks(Integer[] ids) {
        testRankMapper.deleteTestInformations(ids);
        return testRankMapper.deleteTestRanks(ids);
    }

    @Override
    @Transactional
    public int updateTestRank(TestRank testRank) {
        testRankMapper.deleteTestInformation(testRank.getId());
        insertTestInformation(testRank);
        return testRankMapper.updateTestRank(testRank);
    }

    public int insertTestInformation(TestRank testRank) {
        Integer id = testRank.getId();
        List<TestInformation> informationList = testRank.getInformationList();
        informationList.forEach(testInformation -> {
            testInformation.setRankId(id);
        });
        return testRankMapper.insertTestInformations(informationList);
    }
}
